/**
 * 
 */
package com.asdan.seededu.security;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.asdan.seededu.entity.security.Principal;
import com.asdan.seededu.service.UserService;
import com.demo2do.core.cache.CompositeCacheAccessor;

/**
 * @author Downpour
 */
public class SeededuAuthenticationProvider implements UserDetailsService {
	
	private static final Log logger = LogFactory.getLog(SeededuAuthenticationProvider.class);
	
	private CompositeCacheAccessor cacheAccessor;
	
	private UserService userService;
	
	/**
	 * @param cacheAccessor the cacheAccessor to set
	 */
	public void setCacheAccessor(CompositeCacheAccessor cacheAccessor) {
		this.cacheAccessor = cacheAccessor;
	}
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
	 */
	
	@SuppressWarnings("unchecked")
	public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {

		// ======= get user to return =======
		Principal principal =null  ;
		/*
		System.out.println(username) ;
	 = (Principal) userService.getPrincipal(username);
		
		if(principal == null) {
			throw new UsernameNotFoundException("User is not found.");
		}

		logger.info(principal.getName() + "[" + principal.getAuthority() + "] is found to login. ");
		
		// ======= initialize other user properties =======
		
		// 1. initialize role based resources
		Map<String, List<String>> resources = (Map<String, List<String>>) cacheAccessor.evaluate("resources['" + principal.getAuthority() + "']");
		
		if(MapUtils.isNotEmpty(resources)) {
			principal.initResources(resources);
		}
	*/
		return principal;
	}


}
